﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FD.RemoteAssistant.Core;
using FD.RemoteAssistant.Core.Extensions;
using FD.RemoteAssistant.Entities;
using FDRemoteAssistant2.Extensions;
using FDRemoteAssistant2.Manager;

namespace FDRemoteAssistant2
{
    /*
     * CreatedOn: 2018-05-15
     * CreatedBy: Jackie Lee(天宇遊龍)
     */
    public partial class FormSettings : Form
    {
        private const int WM_SYSKEYDOWN = 0x0104;
        private const int WM_KEYDOWN = 0x0100;

        public string OldServer { get; private set; }
        public string OldUserName { get; private set; }

        private bool _isEditing;

        #region 初始化
        public FormSettings()
        {
            InitializeComponent();
            Init();
        }

        public FormSettings(IRemoteSetting remoteSetting)
        {
            InitializeComponent();
            Init(true);
            BindData(remoteSetting);
        }
        private void Init(bool bEdit = false)
        {
            _isEditing = bEdit;
            Text = $"{(bEdit ? "编辑" : "新建")}配置信息 - {Application.ProductName}";
            FormBorderStyle = FormBorderStyle.FixedToolWindow;
            StartPosition = FormStartPosition.CenterParent;
            ckbEnable.Checked = !bEdit;
        }


        #endregion

        #region 逻辑处理
        private void BindData(IRemoteSetting remoteSetting)
        {
            txtServer.Text = remoteSetting.Server;
            txtUserName.Text = remoteSetting.UserName;
            txtPassword.Text = remoteSetting.Password;
            txtDomain.Text = remoteSetting.Domain;
            ckbRedirectDrives.Checked = remoteSetting.RedirectDrives;
            ckbRedirectPorts.Checked = remoteSetting.RedirectPorts;
            ckbRedirectPrinters.Checked = remoteSetting.RedirectPrinters;
            ckbRedirectSmartCards.Checked = remoteSetting.RedirectSmartCards;
            txtName.Text = remoteSetting.Name;
            txtGroup.Text = remoteSetting.Group;
            OldServer = remoteSetting.Server;
            OldUserName = remoteSetting.UserName;
            ckbFullScreen.Checked = remoteSetting.IsFullscreen;
            ckbEnable.Checked = remoteSetting.IsEnable;
            Result = remoteSetting;
        }

        public IRemoteSetting Result { get; private set; }

        private bool Check(Func<bool> action, string tip)
        {
            if (action())
            {
                this.MsgBox(tip);
                return false;
            }

            return true;
        }
        #endregion

        #region 事件处理

        #region 窗体事件
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        Close();
                        return false;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion

        #region 按钮事件
        private void btnOK_Click(object sender, EventArgs e)
        {
            Result = Result ?? new RemoteSetting();

            var oldMenuName = _isEditing ? FDHelper.Current.GetDisplayText(Result) : "";
            var server = txtServer.Text.Trim();
            if (!Check(() => server.IsNullOrEmpty(), "服务器地址不能为空，请输入"))
            {
                txtServer.Focus();
                return;
            }

            var userName = txtUserName.Text.Trim();
            if (!Check(() => userName.IsNullOrEmpty(), "用户名不能为空，请输入"))
            {
                txtUserName.Focus();
                return;
            }
            var password = txtPassword.Text.Trim();
            if (!Check(() => password.IsNullOrEmpty(), "密码不能为空，请输入"))
            {
                txtPassword.Focus();
                return;
            }

            //if (RuntimeContext.Current.Exists(server, userName))
            //{
            //    this.MsgBox($"服务器{server}，用户名{userName}配置已存在");
            //    return;
            //}

            Result.Server = server;
            Result.Domain = txtDomain.Text.Trim();
            Result.Password = password;
            Result.UserName = userName;
            Result.RedirectDrives = ckbRedirectDrives.Checked;
            Result.RedirectPorts = ckbRedirectPorts.Checked;
            Result.RedirectPrinters = ckbRedirectPrinters.Checked;
            Result.RedirectSmartCards = ckbRedirectSmartCards.Checked;
            Result.Group = txtGroup.Text.Trim();
            Result.Name = txtName.Text.Trim();
            Result.IsFullscreen = ckbFullScreen.Checked;
            Result.IsEnable = ckbEnable.Checked;
            DialogResult = DialogResult.OK;

            FDHelper.Current.RemoteConfigChange(Result, _isEditing ? Models.ChangeType.DataModify : Models.ChangeType.DataAdd, oldMenuName);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
            DialogResult = DialogResult.Cancel;
        }

        #endregion
        #endregion

    }
}
